<template>
  <div id="market_freegift_detail">
    <div class="detail-table">
      <Row>
        <Col span="24" class="detail-table__col detail-table__title"><span>飞豆管理详情</span></Col>
      </Row>
      <Row>
        <Col span="12" class="detail-table__col">
          活动编号：{{activityDetail.activityCode}}
        </Col>
        <Col span="12" class="detail-table__col">
          活动名称：{{activityDetail.activityName}}
        </Col>
      </Row>
      <Row>
        <Col span="12" class="detail-table__col">
          开始时间：{{(activityDetail.activityStart || '').replace(/-/g, '/')}}
        </Col>
        <Col span="12" class="detail-table__col">
          结束时间：{{(activityDetail.activityEnd || '').replace(/-/g, '/')}}
        </Col>
      </Row>
      <Row type='flex' style="border-bottom: 1px solid #E9E9E9;">
        <Col span="12" class="detail-table__col" style="display: flex">
          <p style="width: 65px">面向门店：</p>
          <p style="flex: 1">{{activityDetail.faceShop}}</p>
        </Col>
        <Col span="12" class="detail-table__col" style="flex: 1">
          面向会员：{{activityDetail.faseMember}}
        </Col>
      </Row>
      <Row>
        <Col span="24" class="detail-table__col">
          <p v-for="(item, i) in activityDetail.level" :key='i'>{{i + 1}}.飞豆当日累计获得{{item.count}}张，赠送{{item.ticket}}张</p>
        </Col>
      </Row>
      <Row>
        <Col span="24" class="detail-table__col">
          活动状态：{{activityDetail.activityStatus === 0 ? '未开始' : activityDetail.activityStatus === 1 ? '进行中' : activityDetail.activityStatus === 2 ? '已结束' : ''}}
        </Col>
      </Row>
      <Row type='flex' style="border-bottom: 1px solid #E9E9E9;">
        <Col span="12" class="detail-table__col" style="display: flex">
          <p style="width: 85px">微信消息模板：</p>
          <p style="flex: 1">{{message.title}}</p>
        </Col>
        <Col span="12" class="detail-table__col" style="display: flex">
          <p style="width: 85px">微信消息内容：</p>
          <p style="flex: 1">{{message.content}}</p>
        </Col>
      </Row>
      <Row>
        <Col span="12" class="detail-table__col">
          创建时间：{{(activityDetail.createTime || '').replace(/-/g, '/')}}
        </Col>
        <Col span="12" class="detail-table__col">
          编辑时间：{{(activityDetail.updateTime || '').replace(/-/g, '/')}}
        </Col>
      </Row>
      <Row>
        <Col span="12" class="detail-table__col">
          创建人：{{activityDetail.creatorName}}
        </Col>
        <Col span="12" class="detail-table__col">
          编辑人：{{activityDetail.operatorName}}
        </Col>
      </Row>
    </div>
  </div>
</template>

<script>
import { FlyBeanDetail } from '@/api/market/flybean.js'
// import { getMemberLevelList } from '@/api/member/index'
import { getMessages } from '@/api/market/message'
import { getOrgAllTree } from '@/api/system/org'
import axios from 'axios'
export default {
  name: 'flybean_detail',
  data () {
    return {
      activityId: '',
      message: {
        title: '',
        content: ''
      },
      activityDetail: {},
      sendMessages: [],
      memberLevelList: [],
      shopList: []
    }
  },
  methods: {
    // levelFuc (res) {
    //   if (res.data.status === 200) {
    //     this.memberLevelList = res.data.data || []
    //   } else {
    //     this.$Message.error(`${res.data.error}`)
    //   }
    // },
    msgFuc (ennms) {
      if (ennms.data.status === 200) {
        this.sendMessages = [{ templateName: '无', id: '-1' }].concat(ennms.data.data || [])
      } else {
        this.$Message.error(`${ennms.data.error}`)
      }
    },
    shopFuc (tree) {
      if (tree.data.status === 200) {
        this.getStore([tree.data.data])
      } else {
        this.$Message.error(`${tree.data.error}`)
      }
    },
    // 递归取门店
    getStore (list) {
      list.forEach(ele => {
        if (ele.childList.length > 0) {
          this.getStore(ele.childList)
        } else {
          if (ele.orgType === 1) {
            this.shopList.push({
              shopId: ele.id,
              shopName: ele.orgName
            })
          }
        }
      })
    },
    getInit () {
      let levelData = { merchantId: this.$store.state.user.merchantId }
      let msgParam = { ...levelData, templateType: 1, status: 1, suitableType: '20' }
      axios.all([getMessages(msgParam), getOrgAllTree({ ...levelData, orgType: 1 })])
        .then(axios.spread((message, shop) => {
          // this.levelFuc(member)
          this.msgFuc(message)
          this.shopFuc(shop)
          this.getDetail()
        }))
        .catch(err => {
          this.$Message.error(`${err}`)
        })
    },
    getDetail () {
      FlyBeanDetail({ id: this.activityId }).then(res => {
        const result = res.data || {}
        if (result.status === 200) {
          let resultObj = JSON.parse(result.data.activityRule)
          this.activityDetail = {
            activityCode: result.data.activityCode,
            activityName: result.data.activityName,
            activityStart: result.data.activityStart,
            activityEnd: result.data.activityEnd,
            level: resultObj.rules,
            faceShop: resultObj.shopIds.map(item => {
              if (this.shopList.length) {
                this.shopList.forEach(ele => {
                  if (item === ele.shopId) {
                    item = ele.shopName
                  }
                })
              }
              return item
            }).join(','),
            faseMember: resultObj.memberLevelIds.map(item => {
              item = 'V' + item
              return item
            }).join(','),
            templateId: resultObj.templateId,
            activityStatus: result.data.activityStatus,
            createTime: result.data.createTime,
            creatorName: result.data.creatorName,
            updateTime: result.data.updateTime,
            operatorName: result.data.operatorName
          }
          if (resultObj.templateId === '-1') {
            this.message.title = '无'
          } else {
            if (this.sendMessages.length) {
              this.sendMessages.forEach(item => {
                if (item.id === this.activityDetail.templateId) {
                  this.message.title = item.templateName
                  this.message.content = item.content
                }
              })
            }
          }
        } else {
          this.$Message.error(`${result.error}`)
        }
      }).catch(err => {
        this.$Message.error(`${err}`)
      })
    }
  },
  mounted () {
    this.activityId = this.$route.params.id
    this.getInit()
  }
}
</script>

<style lang="less">
@import '~@/assets/stylus/detail-table.less';
#market_freegift_detail {
  padding: 40px 80px;
  .detail-table .detail-table__col {
    border-right: 1px solid #E9E9E9;
    padding: 15px 48px 15px 48px;
    margin-bottom: 0;
  }
}
</style>
